Reference counting is a widely used technique to track resource lifetime and decide when it is safe to delete a resource. A resource can be an object, pointer, or handle, or block of memory, among other things. A reference counter is employed to record when a resource is being used to enable a decision to be made as to when the resource can be destroyed. More particularly, the reference counter can be incremented when a resource is used and decremented when the resource is not needed anymore. If the reference counter reaches zero, the resource can be safely disposed by the last user of the resource since the resource is no longer being used.
Performance issues can occur in a multi-processor computing system when a reference counter is employed with respect to a resource that is commonly used by several processors. The reference counter is conventionally stored in a region of main memory (e.g., RAM). Every time a resource is used, the counter can be incremented in this region of memory. If the resource is utilized frequently, the memory can be incremented quite often. In a very large system with several processors, each processor caches (e.g., level 1 (L1) or level 2 (L2)) the reference counter. However, if a resource is utilized a lot the cache can be invalidated frequently and require reloading. This operation can take time, because the processor has to be called to throw away the cached data and acquire data from main memory.
Conventionally, a compare-and-swap operation is employed for best performance. The compare-and swap operation is an operation for shared memory systems that compares content of a memory location to a specified value, and if the content of the memory location is the same as the specified value, the content of the memory location is updated. There are two ways to implement this operation. The simple way is to take a lock before the counter is incremented, or decremented, and subsequently release the lock. Here, however, performance is hindered by forcing serialization by way of locking. There is hardware that allows counters to be incremented or decremented atomically across all processors. In this case, a lock need not be taken but rather a special compare-and-swap instruction can be called, which will increment the counter, for example, atomically without corrupting the value.